package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dto.interview;
import com.example.dto.result;
import com.example.mapper.InterviewMapper;
import com.example.service.InterviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/interview")
public class InterviewController {
    @Autowired
    private InterviewService interviewService;

    @Autowired
    private InterviewMapper interviewMapper;

    //分页获取招聘会信息
    @GetMapping("/interviewList")
    public result AllList(@RequestParam Integer page, @RequestParam Integer size){
        Page<interview> interviewPage = new Page<>(page,size);
        IPage<interview> res = interviewService.AllList(interviewPage);
        return result.success(res);
    }

    //招聘信息查询
    @GetMapping("/search")
    public result search(@RequestParam Integer page, @RequestParam Integer size, @RequestParam(required = false) String key){
        Page<interview> interviewPage1 = new Page<>(page,size);
        Page<interview> interviewPage2 = new Page<>(page,size);
        if(StringUtils.isEmpty(key)){
            IPage<interview> res = interviewService.AllList(interviewPage1);
            return result.success(res);
        }
        // 用来保存查询结果
        else{
            IPage<interview> res1 = interviewService.findByPosition(interviewPage1,key);
            IPage<interview> res2 = interviewService.findByName(interviewPage2,key);

            if(res1.getRecords().isEmpty()&&!res2.getRecords().isEmpty())
                return result.success(res2);

            if(res2.getRecords().isEmpty()&&!res1.getRecords().isEmpty())
                return result.success(res1);

            System.out.println(res1.getRecords());
            System.out.println(res2.getRecords());
        }

        return result.error("没有数据");
    }

    //新增招聘信息
    @PostMapping("/addInterview")
    public result add(@RequestBody interview i){
        //查找职位ID，有则直接插入，没有则先添加职位信息
        Integer integer = interviewMapper.getPositionID(i.getPositionName(),i.getSalary());
        if(integer != null){
            i.setPositionID(integer);
            //插入employer表
            interviewMapper.addEmployer(i);
            System.out.print(i);
            return result.success(i);
        }else{
            //插入position表
            interviewMapper.addPosition(i);
            integer = interviewMapper.getPositionID(i.getPositionName(),i.getSalary());
            i.setPositionID(integer);
            //插入employer表
            interviewMapper.addEmployer(i);
            System.out.print(i);
            return result.success(i);
        }
    }

    //删除招聘信息
    @DeleteMapping("/deleteInterview")
    public result delete(@RequestParam String name,@RequestParam String position,@RequestParam Double salary){
        Integer id = interviewMapper.getPositionID(position,salary);
        interviewService.delete(name,id);
        return result.success("删除成功");
    }

    //编辑招聘信息
    @PostMapping("/editInterview")
    public result edit(@RequestBody interview i){
        Integer id = interviewMapper.getPositionID(i.getPositionName(),i.getSalary());
        i.setPositionID(id);
        System.out.println(i);
        //修改employer表
        interviewService.editEmploy(i);
        //修改position表
        interviewService.editPosition(i);
        return result.success("修改成功");
    }

}
